<?php

namespace app\repositories\eloquent;
use app\repositories\contracts\MenuInterface;
use app\admin\model\AdminRootGroup;
use app\admin\model\AdminMenu;
use think\response\Json;
use function foo\func;

/***
 * Class MenuProvider
 * @package app\repositories\eloquent
 * @Author Vee7
 * @Date 2021/12/14
 * @Time 13:11
 * @Description 实现后台菜单接口
 */
class MenuProvider implements MenuInterface
{
    protected $allow_menu_id = null;

    /***
     * @var array
     * @Author Vee7
     * @Date 2021/12/23
     * @Time 9:51
     * @Description 所有父类菜单ID和Title
     */
    protected $menuAllParent = [];

    /***
     * @return array
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取菜单分页列表
     */
    public function getMenuListPage($page,$limit,$orderby='sort',$order='desc'):array
    {
        return AdminMenu::limit((($page-1)*$limit),$limit)->order($orderby, $order)->select()->toArray();
    }

    /***
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取菜单总数据量
     */
    public function getMenuListCount():int
    {
        return AdminMenu::count();
    }

    /***
     * @return array
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取菜单列表
     */
    public function getMenuList($orderby='sort',$order='desc'):array
    {
        return AdminMenu::order($orderby, $order)->select()->toArray();
    }

    /***
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 15:56
     * @Description 递归菜单列表按层级转为数组方法
     */
    public function getArrayList($data,$pid = 0):array
    {
        $list = array();
        foreach($data as $v){
            if($v['parent_id'] == $pid){
                $v['child'] = self::getArrayList($data,$v['id']);
                $list[] = $v;
                //dump($list);
            }
        }
        return $list;
    }

    /***
     * @return array
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 16:44
     * @Description 获取菜单数组数据
     */
    public function getList():array
    {
        return self::getArrayList(self::getMenuList());
    }

    /***
     * @param array $id
     * @return bool
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 9:23
     * @Description 按ID删除菜单
     */
    public function delMenuList(array $id):bool
    {
        $list = AdminMenu::select($id);
        return $list->delete();
    }

    /***
     * @param array $data
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 10:43
     * @Description 根据指定参数批量修改菜单字段值
     *
     */
    public function editMenuList(array $data)
    {
        //前端应传递如下格式的二维数组
        //var editData = [{'id':id,'key':kei,'value':value},{...}];
        $menu_obj = new AdminMenu;
        foreach ($data as $k=>$v){
            foreach ($v as $key=>$value){
                $list[] = $value;
            }
        }
        //返回修改后的数据
        return $menu_obj->saveAll($list)->toArray();
    }

    /***
     * @param array $addArray
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 14:48
     * @Description 新增菜单
     */
    public function addMenuList(array $addArray)
    {
        $menu_obj = new AdminMenu;
        $list = [];
        foreach ($addArray as $v){
            //saveAll函数无法自动添加时间戳，手动添加
            $v['create_time'] = date("Y-m-d H:i:s ");
            $list[] = $v;
        }
        return $menu_obj->saveAll($list)->toArray();
    }


    /***
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 15:56
     * @Description 递归菜单列表按层级转为数组方法(返回符合LayUI-Tree数据结构的数组)
     * @ReturnDemo：
     * data = [{
            title: '一级1'
            ,id: 1
            ,field: 'name1'
            ,checked: true
            ,spread: true
            ,children: [{
                title: '二级1-1 可允许跳转'
                ,id: 3
                ,field: 'name11'
                ,href: 'https://www.layuion.com/'
                },{
                title: '二级1-2'
                ,id: 4
                ,spread: true
                ,
                },{
                title: '二级1-3'
                ,id: 20
                ,field: ''
            }]
        }]
     */
    public function getArrayListForAuth($data,$pid = 0,$group_id):array
    {
        $list = array();
        $i = [];

        //获取当前用户组的权限，并重组为数组
        if($this->allow_menu_id == null){
            $this->allow_menu_id = self::getAllowMenuId($group_id);
        }
        $allow_menu_id = explode(',',$this->allow_menu_id['allow_menu_id']);

        foreach($data as $v){
            if($v['parent_id'] == $pid){
                $i['id'] = $v['id'];
                $i['title'] = 'ID:'.$v['id'].' '.$v['title'];
                $i['field'] = $v['route'];
                //根据用户组权限进行判断是否选中
                if($allow_menu_id[0] == 0){
                    $i['checked'] = true;
                }else{
//                    $debugData = [
//                        'allow_menu_id' => $allow_menu_id,
//                        'id' => $v['id'],
//                        'result' => in_array($v['id'],$allow_menu_id)
//                    ];
//                    halt($debugData);
                    if(in_array($v['id'],$allow_menu_id)){
                        $i['checked'] = true;
                    }else{
                        $i['checked'] = false;
                    }
                }
                $i['spread'] = true;
                $i['children'] = self::getArrayListForAuth($data,$v['id'],$group_id);
                $list[] = $i;
            }
        }
//        dump($list);
        return $list;
    }

    /***
     * @Author Vee7
     * @Date 2021/12/16
     * @Time 11:25
     * @Description 获取当前用户组权限
     */
    public function getAllowMenuId($group_id){
        return AdminRootGroup::field('allow_menu_id')->find($group_id)->toArray();
    }

    /***
     * @return array
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 16:44
     * @Description 获取菜单数组数据(返回符合LayUI-Tree数据结构的数组)
     */
    public function getListForAuth($group_id):array
    {
        return self::getArrayListForAuth(self::getMenuList(),0,$group_id);
    }

    /***
     * @param $menu_route
     * @Author Vee7
     * @Date 2021/12/17
     * @Time 9:32
     * @Description 根据路由获取菜单数据
     */
    public function getMenuInfo($menu_route)
    {
        $menu_obj = new AdminMenu;
        return $menu_obj->where('route',$menu_route)->find();
    }

    /***
     * @param $childId
     * @Author Vee7
     * @Date 2021/12/23
     * @Time 9:26
     * @Description 根据子类ID递归获取所有父类数据
     */
    public function getAllParent($childId,$first=true)
    {
        $pData = AdminMenu::field(['id','parent_id','title','route'])->find($childId)->toArray();
        if($first){
            $pData['isChild'] = 1;
        }else{
            $pData['isChild'] = 0;
        }
        array_push($this->menuAllParent,$pData);
        if($pData['parent_id']!=0){
            return self::getAllParent($pData['parent_id'],false);
        }else{
            return $this->menuAllParent;
        }
    }
}